from argus.hijacking.models import *

from django.core.urlresolvers import reverse
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework import status
from djangorestframework.renderers import JSONRenderer
from djangorestframework.permissions import PerUserThrottling

from argus.views import visitorStat

class JSONView(View):
    renderers = (JSONRenderer, )

#class VisitorView(JSONView):
#    def get(self, request):
#        visitors = Visitor.objects.all().order_by('-visitor_id')[:10]
#        vids = [v.visitor_id for v in visitors]
#        if visitors:
#            return [reverse('visitor', kwargs={'id': id}) for id in vids]
#        return Response(status.HTTP_404_NOT_FOUND, [])
#
#class VisitorIdView(JSONView):
#    def get(self, request, id):
#        visitor = Visitor.objects.filter(visitor_id = ("%s" % id))
#        if visitor: return visitor
#        return Response(status.HTTP_404_NOT_FOUND, [])
#
#class CommentView(JSONView):
#    def get(self, request):
#        comments = Comment.objects.all().order_by('-comment_id')[:10]
#        cids = [c.comment_id for c in comments]
#        if comments:
#            return [reverse('comment', kwargs={'id': id}) for id in cids]
#        return Response(status.HTTP_404_NOT_FOUND, [])
#
#class CommentIdView(JSONView):
#    def get(self, request, id):
#        comment = Comment.objects.filter(comment_id = ("%s" % id))
#        if comment: return comment
#        return Response(status.HTTP_404_NOT_FOUND, [])

def getRecentEvents(only_hijacking=True, ibeg=None, iend=None):
    if (ibeg is None) and (iend is None):
        if only_hijacking:
            alarms = Alarm.objects.filter(is_hijacking='Y'
                ).order_by('-alarm_id')[:10]
        else:
            alarms = Alarm.objects.all(
                ).order_by('-alarm_id')[:10]
    else:
        if (ibeg is None) or (iend is None):
            return []
        ibeg = int(ibeg)
        iend = int(iend)
        if ibeg < 0 or iend < 0 or iend <= ibeg or iend - ibeg > 100:
            return []
        if only_hijacking:
            alarms = Alarm.objects.filter(is_hijacking='Y'
                ).order_by('alarm_id')[ibeg:iend]
        else:
            alarms = Alarm.objects.all(
                ).order_by('alarm_id')[ibeg:iend]
    ret = []
    for a in alarms:
        ret.append({
          'timestamp': ("%d" % a.time),
          'prefix': a.prefix_dec,
          'uri': ('/fingerprints/%d/' % a.alarm_id),
          'origin': (a.oldhomeas and ('%d' % a.oldhomeas)) or '',
          'bad_origin': (a.homeas and ('%d' % a.homeas)) or '',
          'bad_path_segment': a.bad_path,
          })
    return ret

class AlarmView(JSONView):
    permissions = (PerUserThrottling,)
    throttle = '1/second'
    def get(self, request, ibeg=None, iend=None):
        visitorStat(request)
        return getRecentEvents(True, ibeg, iend)
    #def post(self, request):
    #    return request

class AnomalousView(JSONView):
    permissions = (PerUserThrottling,)
    throttle = '1/second'
    def get(self, request, ibeg=None, iend=None):
        visitorStat(request)
        return getRecentEvents(False, ibeg, iend)

